#include "adc.h"
#include "ui_adc.h"
#include "control.h"
#include "string"
#include "QDebug"
#include "QString"

#include <QValueAxis>
#include <QSplineSeries> // 曲线
#include <QLineSeries> // 折线




adc::adc(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::adc)
{
    ui->setupUi(this);
    setWindowFlag(Qt::FramelessWindowHint);


}

adc::~adc()
{
    delete ui;
}

void  adc::filer(uint32_t x[], uint32_t y[],int length , uint32_t A)
{

  uint32_t Value1;
  uint32_t NewValue;
  int b=1;
  while(1)
    {
      b+=1;

      Value1 = x[b-1];
      NewValue = x[b];

      if(((NewValue - Value1) > A) || ((Value1 - NewValue) > A))
        {
          y[b-1]=Value1;

        }
      else
        {
           y[b]= x[b];
           y[b-1]=x[b-1];

        }


      if(b >= length)
      {

          break;
      }

  }

}

void adc::buff_left_mov(int *buff_ADC_p,uint32_t (*buff_ADC)[100],int length)
{

    int i; //第一次填满六位数据时，开始循环左移
    if(*buff_ADC_p == 101)//0~8都有数
    {
        for(i=0;i<length;i++)
        {
            *buff_ADC_p = 0;
            while(++(*buff_ADC_p))
            {
                buff_ADC[i][*buff_ADC_p-1]=buff_ADC[i][*buff_ADC_p];
                if(*buff_ADC_p==100)//0~7都有数
                {
                    break;
                }
            }
         }

        *buff_ADC_p=100;
    }

}

void adc::on_pushButton_clicked()
{
    extern QSerialPort *serial;

    if(serial->isOpen()==true)
    {

    serial->write("@");
    serial->write("SADCM");
    serial->write("_");


    serial->write("ADCx");
    if(strcmp(ui->ADCx->currentText().toUtf8(),"ADC1") == 0)
    {
        serial->write("1");
        qDebug() << "ADC1"  << endl;
    }
    else if(strcmp(ui->ADCx->currentText().toUtf8(),"ADC2") == 0)
    {
        serial->write("2");
    }
     else if(strcmp(ui->ADCx->currentText().toUtf8(),"ADC3") == 0)
    {
        serial->write("3");
    }
    serial->write("_");


    serial->write("SUM3");
    serial->write("_");

    //  qDebug() <<  ui->radioButton->isChecked()  << endl;
       if(ui->channel0->isChecked() == true)
       {
           serial->write("IN0");
            serial->write("_");

       }
        if(ui->channel1->isChecked() == true)
       {
            serial->write("IN1");
             serial->write("_");

       }
        if(ui->channel2->isChecked() == true)
       {
            serial->write("IN2");
             serial->write("_");

       }
        if(ui->channel3->isChecked() == true)
       {
            serial->write("IN3");
             serial->write("_");

       }
        if(ui->channel4->isChecked() == true)
       {
            serial->write("IN4");
             serial->write("_");

       }
        if(ui->channel5->isChecked() == true)
       {
            serial->write("IN5");
             serial->write("_");

       }
        if(ui->channel6->isChecked() == true)
       {
            serial->write("IN6");
             serial->write("_");
              qDebug() << "IN6"  << endl;

       }
        if(ui->channel7->isChecked() == true)
       {
            serial->write("IN7");
             serial->write("_");

       }
        if(ui->channel8->isChecked() == true)
       {
            serial->write("IN8");
             serial->write("_");
             qDebug() << "IN8"  << endl;

       }
        if(ui->channel9->isChecked() == true)
       {
            serial->write("IN9");
             serial->write("_");
              qDebug() << "IN9"  << endl;

       }
        if(ui->channel10->isChecked() == true)
       {
            serial->write("IN10");
             serial->write("_");

       }
        if(ui->channel11->isChecked() == true)
       {
            serial->write("IN11");
             serial->write("_");

       }
        if(ui->channel12->isChecked() == true)
       {
            serial->write("IN12");
             serial->write("_");

       }
        if(ui->channel13->isChecked() == true)
       {
            serial->write("IN13");
             serial->write("_");

       }
        if(ui->channel14->isChecked() == true)
       {
            serial->write("IN14");
             serial->write("_");

       }
        if(ui->channel15->isChecked() == true)
       {
            serial->write("IN15");
             serial->write("_");

       }

    serial->write("_");
    serial->write(" ");
    serial->write(".");
    }


    extern int tcp_state;
    if(tcp_state)
    {
        extern QTcpSocket *qtcpsocket;
        qtcpsocket->write("@");
        qtcpsocket->write("SADCM");
        qtcpsocket->write("_");


        qtcpsocket->write("ADCx");
        if(strcmp(ui->ADCx->currentText().toUtf8(),"ADC1") == 0)
        {
            qtcpsocket->write("1");
 //           qDebug() << "ADC1"  << endl;
        }
        else if(strcmp(ui->ADCx->currentText().toUtf8(),"ADC2") == 0)
        {
            qtcpsocket->write("2");
        }
         else if(strcmp(ui->ADCx->currentText().toUtf8(),"ADC3") == 0)
        {
            qtcpsocket->write("3");
        }
        qtcpsocket->write("_");


        qtcpsocket->write("SUM3");
        qtcpsocket->write("_");

        //  qDebug() <<  ui->radioButton->isChecked()  << endl;
           if(ui->channel0->isChecked() == true)
           {
               qtcpsocket->write("IN0");
                qtcpsocket->write("_");

           }
            if(ui->channel1->isChecked() == true)
           {
                qtcpsocket->write("IN1");
                 qtcpsocket->write("_");

           }
            if(ui->channel2->isChecked() == true)
           {
                qtcpsocket->write("IN2");
                 qtcpsocket->write("_");

           }

            if(ui->channel3->isChecked() == true)
           {
                qtcpsocket->write("IN3");
                 qtcpsocket->write("_");

           }
            if(ui->channel4->isChecked() == true)
           {
                qtcpsocket->write("IN4");
                 qtcpsocket->write("_");

           }
            if(ui->channel5->isChecked() == true)
           {
                qtcpsocket->write("IN5");
                 qtcpsocket->write("_");

           }
            if(ui->channel6->isChecked() == true)
           {
                qtcpsocket->write("IN6");
                 qtcpsocket->write("_");
                  qDebug() << "IN6"  << endl;

           }
            if(ui->channel7->isChecked() == true)
           {
                qtcpsocket->write("IN7");
                 qtcpsocket->write("_");

           }
            if(ui->channel8->isChecked() == true)
           {
                qtcpsocket->write("IN8");
                 qtcpsocket->write("_");
                 qDebug() << "IN8"  << endl;

           }
            if(ui->channel9->isChecked() == true)
           {
                qtcpsocket->write("IN9");
                 qtcpsocket->write("_");
                  qDebug() << "IN9"  << endl;

           }
            if(ui->channel10->isChecked() == true)
           {
                qtcpsocket->write("IN10");
                 qtcpsocket->write("_");

           }
            if(ui->channel11->isChecked() == true)
           {
                qtcpsocket->write("IN11");
                 qtcpsocket->write("_");

           }
            if(ui->channel12->isChecked() == true)
           {
                qtcpsocket->write("IN12");
                 qtcpsocket->write("_");

           }
            if(ui->channel13->isChecked() == true)
           {
                qtcpsocket->write("IN13");
                 qtcpsocket->write("_");

           }
            if(ui->channel14->isChecked() == true)
           {
                qtcpsocket->write("IN14");
                 qtcpsocket->write("_");

           }
            if(ui->channel15->isChecked() == true)
           {
                qtcpsocket->write("IN15");
                 qtcpsocket->write("_");

           }

        qtcpsocket->write("_");
        qtcpsocket->write(" ");
        qtcpsocket->write(".");
    }



}




void adc::adc_play()
{
    static int x =0;
    static uint32_t buff_ADC[100][100],i;
    static int buff_ADC_p=0;
    static char * buff_p = NULL ;//
    static char * buff_p1 = NULL ;//
    static char * buff_p2 = NULL ;//

    // 2.创建一个图表
    QChart *chart = new QChart();

    // 3.创建坐标轴
    QValueAxis *valueAxisX = new QValueAxis();
    QValueAxis *valueAxisY = new QValueAxis();



    const char * buff;




    //显示数值
    extern QSerialPort *serial;
    extern QByteArray getcmd;
    if(serial->isOpen()==true && strcmp(getcmd,"@adcOK ."))
    {

       ui->textEdit->setText(getcmd);
        buff = QString(getcmd).toStdString().c_str();
    }
    else
    {

        buff=NULL;
    }

    extern int tcp_state;
    extern QByteArray TCP_buff;
    if(tcp_state)
    {
        qDebug()  <<TCP_buff   << endl;

        ui->textEdit->setText(TCP_buff);
        buff = QString(TCP_buff).toStdString().c_str();
    }

    //类型转换成数组

    if(buff)
    {
        buff_p= strstr(buff,"X0");
        buff_p1= strstr(buff,"X1");
        buff_p2= strstr(buff,"X2");


//    ui->textEdit->append(buff_p1);
//    ui->textEdit->append(buff_p2);

    if(buff_p != NULL)
   {
        buff_p+=2;
        //字符串转换成uint32
        buff_ADC[0][buff_ADC_p] =  atoi(buff_p);
   }

  if(buff_p1 != NULL)
 {
      buff_p1+=2;

      //字符串转换成uint32
      buff_ADC[1][buff_ADC_p] =  atoi(buff_p1);
 }


  if(buff_p2 != NULL)
 {
      buff_p2+=2;

      //字符串转换成uint32
      buff_ADC[2][buff_ADC_p] =  atoi(buff_p2);

 }
 buff_ADC_p++;



     buff_left_mov(&buff_ADC_p, buff_ADC,3);
//   buff_left_mov(&buff_ADC_p, buff_ADC[2]);
//   filer(buff_ADC,buff_N,100,100);

    // 4.设置坐标轴的范围
    if(x>=1000)
    {
        valueAxisX->setRange(x, 1000+x);
    }
    else
    {
        valueAxisX->setRange(0, 1000);
    }
    valueAxisY->setRange(0, 4096);




    // 5.设置坐标轴的标题和显示的格式
    valueAxisX->setTitleText("时间/ms");
    valueAxisY->setTitleText("采样值");
    valueAxisX->setLabelFormat("%d");
    valueAxisY->setLabelFormat("%d");

    // 设置标题的字体大小
    QFont font;
    font.setPointSize(16); // 设置为16号字体
    // 如果你想要设置字体为粗体
    font.setBold(true);

    valueAxisX->setLabelsFont(font);
    valueAxisY->setLabelsFont(font);



    // 设置xy的显示的格式
      valueAxisX->setTickCount(6);
      valueAxisY->setTickCount(5);
    // 6.图表添加坐标轴
    chart->createDefaultAxes();
    chart->addAxis(valueAxisX, Qt::AlignBottom);
    chart->addAxis(valueAxisY, Qt::AlignLeft);

    // 7.设置图表的标题以及图例显示是否需要
    chart->setTitle("采样值与时间曲线");
    chart->legend()->setVisible(false);




    // 8.创建曲线对象添加它的点，设置曲线的颜色
    QLineSeries *lineSeries =  new QLineSeries();
    if(x>=1000)
    {
        for(i=0;i<100;i++)
        {
            lineSeries->append(10*i+x,  buff_ADC[0][i]);
        }

    }
    else
    {

        for(i=0;i<100;i++)
        {
            lineSeries->append(10*i,  buff_ADC[0][i]);
        }
    }


    QPen pen(QColor(0xff5566));
    lineSeries->setPen(pen);

    // 9.图表添加曲线
    chart->addSeries(lineSeries);
    // 10.!!!!将曲线的数据与坐标轴相连！！！！注意，这个要在图表添加曲线之后
    // 附属到坐标轴上面去
    lineSeries->attachAxis(valueAxisX);
    lineSeries->attachAxis(valueAxisY);



    // 8.创建曲线对象添加它的点，设置曲线的颜色
    QLineSeries *lineSeries2 =  new QLineSeries();
    if(x>=1000)
    {
        for(i=0;i<100;i++)
        {
            lineSeries2->append(10*i+x,  buff_ADC[1][i]);
        }

    }
    else
    {

        for(i=0;i<100;i++)
        {
            lineSeries2->append(10*i,  buff_ADC[1][i]);
        }
    }


    QPen pen2(QColor(0x00BFFF));
    lineSeries2->setPen(pen2);
    qDebug() <<  buff_ADC[1][2] << endl;
    // 9.图表添加曲线
    chart->addSeries(lineSeries2);    // 10.!!!!将曲线的数据与坐标轴相连！！！！注意，这个要在图表添加曲线之后
    // 附属到坐标轴上面去
    lineSeries2->attachAxis(valueAxisX);
    lineSeries2->attachAxis(valueAxisY);



    // 8.创建曲线对象添加它的点，设置曲线的颜色
    QLineSeries *lineSeries3 =  new QLineSeries();
    if(x>=1000)
    {
        for(i=0;i<100;i++)
        {
            lineSeries3->append(10*i+x,buff_ADC[2][i]);
        }

    }
    else
    {

        for(i=0;i<100;i++)
        {
            lineSeries3->append(10*i,buff_ADC[2][i]);
        }
    }



    QPen pen3(QColor(0x9A009A));
    lineSeries3->setPen(pen3);

    // 9.图表添加曲线
    chart->addSeries(lineSeries3);

    lineSeries3->attachAxis(valueAxisX);
    lineSeries3->attachAxis(valueAxisY);



    // 11.将图表放置于图表视图
    ui->ADC_chart->setChart(chart);




      x+=10;
    }
}


void adc::on_pushButton_2_clicked()
{
    extern int adc_state;
    extern QSerialPort *serial;
    if(serial->isOpen()==true)
    {
        serial->write("@close .");
    }


    extern int tcp_state;
    extern QTcpSocket *qtcpsocket;
    if(tcp_state == 1)
    {
        qtcpsocket->write("@close .");
    }
    adc_state =0;
    this->close();

}










